package com.woniuxy.controller;


import com.woniuxy.entity.Menu;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.MenuService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2021-05-15 14:52:37
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Menu selectOne(Integer id) {
        return this.menuService.queryById(id);
    }

    /**
     * Description : 获取所有分级菜单  <br/>
     * ChangeLog : 1. 创建 ( 11:39 [Hello Mr. Guo]);
     *
     * @return java.util.List<com.woniuxy.entity.Menu>
    */

    @GetMapping("findTopMenu")
    public List<Menu> findTopMenu(){
        List<Menu> topMenu = this.menuService.findTopMenu();
        return topMenu;
    }


    /**
     * Description : 查询所有父级菜单  <br/>
     * ChangeLog : 1. 创建 ( 11:49 [Hello Mr. Guo]);
     *
     * @return java.util.List<com.woniuxy.entity.Menu>
    */

    @GetMapping("findFirstMenu")
    public List<Menu> findFirstMenu(){
        List<Menu> menus = this.menuService.findfirstMenu();
        return menus;
    }


    /**
     * Description : 从前端收到更改菜单的请求，更改数据库menu的数据  <br/>
     * ChangeLog : 1. 创建 ( 11:50 [Hello Mr. Guo]);
     * @param menu
     * @return String
    */
    @PostMapping("save")
    public String save(@RequestBody  Menu menu){
        int i = this.menuService.update(menu);
        if (i>0){
            return "success";
        }
        return "failure";
    }

    //@RequiresRoles("1")
    @PostMapping("delete")
    public String delete(@RequestBody Menu menu){
        System.out.println(menu);
        Integer menuId = menu.getMenuId();
        boolean b = this.menuService.deleteById(menuId);
        if (b){
            return "success";
        }
        return "failure";
    }

}
